//
//  MainViewController.swift
//  XYWeibo
//
//  Created by 常晓璇 on 6/23/15.
//  Copyright © 2015 chang xiaoxuan. All rights reserved.
//

import UIKit
import FFAutoLayout

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setValue(MainTabBar(), forKey: "tabBar")
        addChildViewControllers()
        // Do any additional setup after loading the view.
        
        
        
    }

    private func addChildViewControllers() {
        
        // 加载json
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
        let data = NSData(contentsOfFile: path)!
        
        // 反序列化
        do {
            let array = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
            
            for dict in array as! [[String : String]] {
                addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
            }
        } catch (_) {
            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            addChildViewController("DiscoverTableViewController", title: "寻找", imageName: "tabbar_discover")
            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }
        
        
    }
    
    private func addChildViewController(vcName:String, title:String, imageName:String) {
        
//        tabBar.tintColor = UIColor.orangeColor()
        
        let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        // 字符串转成类
        let cls :AnyClass = NSClassFromString(ns + "." + vcName)!
        // 实例化对象
        let vc = cls.new() as! UIViewController
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
        
    }


}
